<?php
/**
 * Created by PhpStorm.
 * User: HuangLuSen
 * Date: 2017/5/11
 * Time: 11:29
 */

namespace app\common\traits;

use app\common\message\model\SmsMessage;
use app\common\message\type\SmsType;
use app\common\model\Wesmslog;

use app\common\aliyun\Aliyunmsg;

trait SendMessage
{


    /**    以前的阿里大于
     * 发送微信模板消息
     * @param $data ['phone'=>'xxx','param'=>['xx'=>'xxx'],'tempcode'=>'xxx']
     * @return bool
     */
//    public function sendSmss($data){
//        $wechattype = new SmsType();
//        $wechatmessage = new SmsMessage($wechattype,$data);
//        $wesmslog = new Wesmslog();
//        $str = '';
//        foreach ($data as $key=>$v){
//           if(is_array($v)){
//                foreach ($v as $keys=>$vo){
//                    $str.=$keys.":".$vo.';';
//                }
//           }else{
//               $str.=$key.":".$v.';';
//           }
//        }
//        $arr['send_id']     = $data['sendid'];
//        $arr['receive_id']  = $data['phone'];
//        $arr['order_id']    = $data['orderid'];
//        $arr['log_type']    = 2;
//        $arr['send_msg']    = $str;
//        $array =  $wechatmessage->sendMessage();
//        $result = true;
//        $strs='';
//        if(is_array($array['result']) && isset($array['result'])){
//                if (!$array['result']['success']){
//                    $arr['is_success']  = 2;
//                    $arr['error_msg']   ='错误代码：'.$array['result']['err_code'];
//                    $result = false;
//                }
//            }else{
//                $arr['is_success']  = 2;
//                foreach ($array as $key=>$v){
//                    $strs.= $key.":".$v.';';
//                }
//                $arr['error_msg']   =$strs;
//            $result = false;
//        }
//        $wesmslog->save($arr);
//        return $result;
//
//    }

    /**  现在使用的阿里云云通信
     * @param $data
     * @return bool
     */
    public function sendSmss($data){
        $aliyun = new Aliyunmsg();
        $wesmslog = new Wesmslog();

        $str = '';
        foreach ($data as $key=>$v){
            if(is_array($v)){
                foreach ($v as $keys=>$vo){
                    $str.=$keys.":".$vo.';';
                }
            }else{
                $str.=$key.":".$v.';';
            }
        }
        $arr['send_id']     = $data['sendid'];
        $arr['receive_id']  = $data['phone'];
        $arr['order_id']    = $data['orderid'];
        $arr['log_type']    = 2;
        $arr['send_msg']    = $str;
        $array =  $aliyun->sendSms($data['tempcode'],$data['phone'],$data['param']);
        $result = true;
        if($array->Code != 'OK'){
            $arr['is_success']  = 2;
            $arr['error_msg']   ='错误代码：'.$array->Code.'错误信息：'.$array->Message;
            $result = false;
        }
        $wesmslog->save($arr);
        return $result;
    }
    
}